package org.rabbitmq.mq02.mqServer.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建交换机：
 */
@Data
public class Exchange {
    //交换机的唯一标识
    private String name;
    //交换机类型：默认为直接交换件
    private ExchangeType type = ExchangeType.DIRECT;
    //是否持久化 ，默认持久化
    private boolean durable = true;
    //在交换机不再使用的时候，是否自动删除 ,默认不自动删除
    private boolean autoDelete = false;
    //选填参数
    private Map<String,Object> args = new HashMap<>();

    //这里的args的数据类型是Map,数据库中没有对应的数据类型,
    // 存入数据库时,要转换成String类型,
    //从数据库中读取时,要转换成Map类型
    public String getArgs(){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(args);
        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return "{}";
    }
    public void setArgs(String args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        this.args = mapper.readValue(args, new TypeReference<HashMap<String,Object>>() {});
    }

    public void setArgs(Map<String,Object> args){
        this.args = args;
    }

}
